package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 清空购物车功能
     */
    public void cleanCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        shoppingCartMapper.DeleteCart(shoppingCart);
    }

    /**
     * 添加购物车的service实现
     * @param shoppingCartDTO
     */

    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断要加入购物车的商品是否存在
        //select * from shopping_cart where user_id = ? and setmeal_id = ?
        //select * from shopping_cart where user_id = ? and dish_id = ?? and dish_flavor = ??
        //通过mybatis动态sql即可
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.Query(shoppingCart);

        //如果要添加的商品已经存在了 就直接update数量即可
        if(shoppingCarts != null && shoppingCarts.size() >0 ){
            //虽然是List集合 但是按照条件查询只有可能查不到 或者只能查到一条数据 所以只要获取第一条即可
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            //update shopping_cart set number = ?? where id = ??
            shoppingCartMapper.UpdateNumberById(shoppingCart1);
        }else{
            //如果商品不存在 需要插入一条购物车数据
            //需要先知道添加的到底是菜品还是套餐 再决定具体去查哪个表 补全这个购物车的类
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null ){
                //本次添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //本次添加的就是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //重复的语句 直接拿到外面来 解耦
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //最后统一插入数据即可
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        List<ShoppingCart> lists = shoppingCartMapper.Query(shoppingCart);
        return lists;
    }

    /**
     * 删除购物车中的内容
     * @param shoppingCartDTO
     */
    public void deleteItem(ShoppingCartDTO shoppingCartDTO) {
        shoppingCartMapper.deleteItem(shoppingCartDTO);
    }
}
